package com.bdqn.ch02.JD.jd2;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * XMLSelect
 *
 * 使用DOM 进行对文档的查询
 * @aurhor Administrator  whs
 * @since 2024/7/1
 */
public class XMLSelect {

    public static void main(String[] args) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse("src/com/bdqn/ch02/JD/成绩信息.xml");
            NodeList list = doc.getElementsByTagName("student");
            for (int i = 0; i < list.getLength(); i++) {
                Node student = list.item(i);
                Element studentEle = (Element) student;
                String studentStr = studentEle.getAttribute("id");
                NodeList types = studentEle.getChildNodes();
                System.out.println("学号：" + studentStr);
                for (int j = 0; j < types.getLength(); j++) {
                    Node type = types.item(j);
                    if (type.getNodeType() == Node.ELEMENT_NODE) {
                        Element typeEle = (Element) type;
                        String textStu = type.getFirstChild().getNodeValue();
                        System.out.println("成绩：" + textStu);
                    }
                }

                System.out.println();
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }
    /**
     * 学号：1
     * 成绩：帅帅
     * 成绩：100
     * 成绩：语文
     *
     * 学号：2
     * 成绩：阿贡
     * 成绩：110
     * 成绩：数学
     *
     * 学号：3
     * 成绩：听封
     * 成绩：60
     * 成绩：英语
     */

}
